package com.qdsg.ysg.doctor.pharmacist.activity;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;
import com.qdsg.ysg.doctor.ui.adapter.MedicineAdapter;
import com.qdsg.ysg.doctor.ui.adapter.SecondAdapter;
import com.qdsg.ysg.doctor.ui.dialog.CheckDialog;
import com.qdsg.ysg.doctor.util.ButtonUtil;
import com.qdsg.ysg.doctor.util.ImageLoaderHelper;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.response.BaseResponse;
import com.rest.response.CheckResponse;
import com.rest.response.PrescriptionResponse;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 处方审核列表点进去的处方详情
 * * Created by HP on 2020/3/2.
 */

public class CheckActivity extends BaseActivity {

    @BindView(R.id.img_head)
    ImageView img_head;
    @BindView(R.id.tv_doctor_name)
    TextView tv_doctor_name;

    @BindView(R.id.tv_message)
    TextView tv_message;
    @BindView(R.id.tv_patient_name)
    TextView tv_patient_name;
    @BindView(R.id.tv_patient_detail)
    TextView tv_patient_detail;
    @BindView(R.id.tv_advice)
    TextView tv_advice;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_notice)
    TextView tv_notice;
    @BindView(R.id.tv_check_status)
    TextView tv_check_status;
    @BindView(R.id.edt_reason)
    EditText edt_reason;

    @BindView(R.id.pre)
    TextView pre;
    @BindView(R.id.main)
    TextView main;
    @BindView(R.id.tail)
    TextView tail;
    @BindView(R.id.check_container)
    LinearLayout check_container;

    @BindView(R.id.second_recyclerView)
    RecyclerView second_recyclerView;

    SecondAdapter secondAdapter;
    List<PrescriptionResponse.Second> list2 = new ArrayList<>();

    CheckResponse.Check check;
    List<PrescriptionResponse.Prescription> list = new ArrayList<>();
    MedicineAdapter adapter;
    String status;
    String prescriptionId, preAppendId, icdId, tailAppendId;

    @SuppressLint("SetTextI18n")
    @Override
    protected void initViews() {
        tv_message.setText( check.diagType );
        tv_doctor_name.setText( check.docName );
        tv_patient_name.setText( check.patientName );
        tv_patient_detail.setText( check.sex + " " + check.pAge );
        LinearLayoutManager manager = new LinearLayoutManager( this );
        manager.setOrientation( LinearLayoutManager.VERTICAL );
        recyclerView.setLayoutManager( manager );
        adapter = new MedicineAdapter( this, list );
        recyclerView.setAdapter( adapter );

        LinearLayoutManager manager2 = new LinearLayoutManager( this );
        manager2.setOrientation( LinearLayoutManager.VERTICAL );
        second_recyclerView.setLayoutManager( manager2 );
        secondAdapter = new SecondAdapter( this, list2 );
        second_recyclerView.setAdapter( secondAdapter );
    }

    @Override
    protected void initData() {
        check = (CheckResponse.Check) getIntent().getSerializableExtra( "obj" );
        getDetail();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.aty_prescription_check;
    }


    private void getDetail() {
        RestProxy.getInstance().getPrescription( check.id, new Observer<PrescriptionResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onNext(PrescriptionResponse prescriptionResponse) {
                list.clear();
                list.addAll( prescriptionResponse.data.westernList );
                adapter.notifyDataSetChanged();
                tv_advice.setText( "医生建议：" + prescriptionResponse.data.diagnoseResult );
                tv_notice.setText( "用药嘱托：" + prescriptionResponse.data.remark );
                prescriptionId = prescriptionResponse.data.prescriptionId;
                list2.clear();
                list2.addAll( prescriptionResponse.data.secondList );
                secondAdapter.notifyDataSetChanged();
                pre.setText( prescriptionResponse.data.preAppend );
                main.setText( prescriptionResponse.data.icd );
                tail.setText( prescriptionResponse.data.tailAppend );
                ImageLoaderHelper.getInstance().GlideImageLoader( mContext, prescriptionResponse.data.url, img_head, R.mipmap.img_default );
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( mContext, e );
            }

            @Override
            public void onComplete() {

            }
        } );
    }

    @OnClick(R.id.tv_check_status)
    void tv_check_status() {
        showSelectHeadPicDialog();
    }

    @OnClick(R.id.btn_confirm)
    void btn_confirm() {
        check();
    }

    private CheckDialog mChecklDialog = null;


    private void showSelectHeadPicDialog() {
        if (mChecklDialog == null) {
            // 初始化点击头像弹出的对话框
            mChecklDialog = CheckDialog.newInstance();
            mChecklDialog.setOnItemClickListener( new CheckDialog.OnItemClickListener() {
                @Override
                public void check() {
                    tv_check_status.setText( "审核通过" );
                    status = "1";
                    check_container.setVisibility( View.GONE );
                }

                @Override
                public void refuse() {
                    tv_check_status.setText( "审核拒绝" );
                    status = "2";
                    check_container.setVisibility( View.VISIBLE );
                }
            } );
        }
        mChecklDialog.show( getSupportFragmentManager(), "mChecklDialog" );
    }


    private void check() {
        if (status == null) {
            ToastUtil.showToast( mContext, "请选择审核意见" );
            return;
        }
        RestProxy.getInstance().check( prescriptionId, status, edt_reason.getText().toString(), new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {
                ButtonUtil.setButtonStatus( mContext, R.id.btn_confirm, false );
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                finish();
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( mContext, e );
                ButtonUtil.setButtonStatus( mContext, R.id.btn_confirm, true );
            }

            @Override
            public void onComplete() {

            }
        } );
    }
}
